<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Post */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="post-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'tags')->textarea(['rows' => 6]) ?>

    <?php
    //第一种方法
    //$objArray = \common\models\Poststatus::find()->all();
    //$allStatus = \yii\helpers\ArrayHelper::map($objArray, 'id', 'name');

    //第二种方法
    //$objArray = Yii::$app->db->createCommand('select id,name from poststatus WHERE 1')->queryAll();
    //$allStatus = \yii\helpers\ArrayHelper::map($objArray, 'id', 'name');
    //var_dump($allStatus);
    //第三种方法 查询构建器
    //$allStatus = (new \yii\db\Query())->select(['name','id'])->from('poststatus')->indexBy('id')->column();
    //第四重方法
    $allStatus = \common\models\Poststatus::find()->select(['name','id'])->orderBy('position')->indexBy('id')->column();

    //获取作者信息
    $userArray = \common\models\User::find()->select(['username','id'])->indexBy('id')->column();

    ?>
    <?= $form->field($model, 'status')->dropDownList($allStatus, ['prompt' => '请选择状态']) ?>

    <?= $form->field($model, 'author_id')->dropDownList($userArray, ['prompt' => '请选择作者']);?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '添加' : '修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
